package cn.wx.basic.service.impl;

import cn.wx.basic.exception.BusinessException;
import cn.wx.basic.service.IVerifyCodeService;
import cn.wx.basic.util.SmsUtils;
import cn.wx.basic.util.StrUtils;
import cn.wx.basic.util.VerifyCodeUtils;
import cn.wx.user.domain.User;
import cn.wx.user.mapper.UserMapper;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Override
    public String getVerifyCode(String key) {
        //1.判断
        if(StringUtils.isEmpty(key)){
            throw  new BusinessException("不能为空");
        }

        //2.获取随机验证码验证码
        String complexRandomString = StrUtils.getComplexRandomString(4);

        //把验证码存到redis中并设置过期时间
        redisTemplate.opsForValue().set(key,complexRandomString,3, TimeUnit.DAYS);

        //3.获取验证码图形
        String code = VerifyCodeUtils.verifyCode(100, 30, complexRandomString);
        return code;
    }


    /*
    *   点击获取手机验证码
    * */
    @Override
    public void sendMobileCode(Map<String, String> map) {

        //把前端传来的map拿出来
        String phone = map.get("phone");   //电话
        String imageCode = map.get("imageCode");  //验证码
        String imageCodeKey = map.get("imageCodeKey"); //本地的uuid 的key值

        //1.判断是否为空
        if(
                StringUtils.isEmpty(phone)
                || StringUtils.isEmpty(imageCode)
                || StringUtils.isEmpty(imageCodeKey)
        ){
            throw new BusinessException("输入不能为空");
        }

        //2.图形验证码验证
        Object o = redisTemplate.opsForValue().get(imageCodeKey);
        if(o==null){
            throw new BusinessException("图形验证码过期了,请重新输入");
        }
        //验证码不区别大小写
        if(!o.toString().equalsIgnoreCase(imageCode)){
            throw new BusinessException("图形验证码输入错误,请重新输入");
        }

        //3.手机验证码是否被注册过
        User user = userMapper.findByPhone(phone);
        if(user!=null){   //已经注册过
            throw new BusinessException("手机号已被注册");
        }

        //4.判断是否是第一次发请求，或者是验证码已经过期 从redis里面取
        Object value = redisTemplate.opsForValue().get(phone + "phoneRegisterCode");

        String code = null;
        if(value==null){
            code = StrUtils.getComplexRandomString(6); //创建一个验证码
        }else {
            String redisTime = value.toString().split(":")[1]; // 获取创建时候的时间戳
            Long aLong = Long.valueOf(redisTime);
            if(System.currentTimeMillis()-aLong<1*60*1000){   //如果相隔时间小于1分钟
                throw new BusinessException("请一分钟以后再试");
            }else {  //如果大于1分钟
                code = value.toString().split(":")[0]; //获取前面的key
            }
        }
            //5.把code存入redis中
            redisTemplate.opsForValue().set(
                    phone+"phoneRegisterCode",   //key
                    code+":"+System.currentTimeMillis(),  //value
                    3,
                    TimeUnit.MINUTES
            );

            //6.发送短信
            //SmsUtils.sendSms(phone,"您的验证码为:"+code+"请在三分钟之类使用");
            System.out.println("您的验证码为:"+code+"请在三分钟之类使用");


    }

    @Override
    public void binderSmsCode(Map<String, String> map) {

        //把前端传来的map拿出来
        String phone = map.get("phone");   //电话

        //1.判断是否为空
        if(
                StringUtils.isEmpty(phone)

        ){
            throw new BusinessException("手机号不能为空");
        }




        //4.判断是否是第一次发请求，或者是验证码已经过期 从redis里面取
        Object value = redisTemplate.opsForValue().get(phone + "wxbinder");

        String code = null;
        if(value==null){
            code = StrUtils.getComplexRandomString(6); //创建一个验证码
        }else {
            String redisTime = value.toString().split(":")[1]; // 获取创建时候的时间戳
            Long aLong = Long.valueOf(redisTime);
            if(System.currentTimeMillis()-aLong<1*60*1000){   //如果相隔时间小于1分钟
                throw new BusinessException("请一分钟以后再试");
            }else {  //如果大于1分钟
                code = value.toString().split(":")[0]; //获取前面的key
            }
        }
        //5.把code存入redis中
        redisTemplate.opsForValue().set(
                phone+"wxbinder",   //key
                code+":"+System.currentTimeMillis(),  //value
                3,
                TimeUnit.MINUTES
        );

        //6.发送短信
        //SmsUtils.sendSms(phone,"您的验证码为:"+code+"请在三分钟之类使用");
        System.out.println("您的验证码为:"+code+"请在三分钟之类使用");


    }
}
